package com.kerberos;

import java.util.ArrayList;

import com.kerberos.R;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

public class EditarPaneles extends DialogFragment implements
		OnItemSelectedListener {

	panel mipanel;
	boolean editando;
	int miposicion;
	String nombreAntiguo;

	EditarPaneles(plc Plc, int posicion, boolean edit) {
		editando = edit;
		miposicion = posicion;
		if (editando) {
			this.mipanel = Plc.paneles.get(posicion);
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View rootView = inflater.inflate(R.layout.editar_paneles, null);
		final EditText editar = (EditText) rootView
				.findViewById(R.id.nombre_panel);
		if (editando) {
			editar.setText(mipanel.titulo.getText());
			nombreAntiguo = mipanel.titulo.getText().toString();
		}

		builder.setView(rootView);
		// Add action buttons
		builder.setPositiveButton(R.string.aceptar,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int id) {
						if (editando) {
							mipanel.titulo.setText(editar.getText());
							servidor.plcs.get(MainActivity.pagina).paneles.set(
									miposicion, mipanel);
							for (int i = 0; i < servidor.plcs
									.get(MainActivity.pagina).variables.size(); i++) {
								String n = servidor.plcs
										.get(MainActivity.pagina).variables
										.get(i).panel;
								if (n.equals(nombreAntiguo))
									servidor.plcs.get(MainActivity.pagina).variables
											.get(i).panel = editar.getText().toString();
							}
						} else {
							mipanel = new panel(MainActivity.ctx);
							mipanel.titulo.setText(editar.getText());
							servidor.plcs.get(MainActivity.pagina).paneles
									.add(mipanel);
						}
						xml.generarServidor();
						xml.escribirXml(MainActivity.ctx);
					}
				});
		builder.setNegativeButton(R.string.cancelar,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						EditarPaneles.this.getDialog().cancel();
					}
				});
		AlertDialog b = builder.create();
		b.show();
		// Button okButton = b.getButton(AlertDialog.BUTTON_POSITIVE);
		// okButton.setEnabled(false);

		return b;

	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub

	}
}
